package de.micromata.jira.rest.action;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Assert;

import de.micromata.jira.rest.JiraRestClient;
import de.micromata.jira.rest.core.domain.CommentBean;
import de.micromata.jira.rest.core.util.RestException;

/**
 * Servlet implementation class Test
 */
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;
	HttpServletRequest request;
    static final String USERNAME_TO_SEARCH = "qianyf1013";//创建者
    static final String ISSUEKEY_TO_SEARCH = "DEMO-1";//问题名
    static final String PROJECT_TO_SEARCH = "DEMO";//项目名
    static final String SUCCESS = "success";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    static JiraRestClient jiraRestClient;

    
    public void connect() throws URISyntaxException, IOException, ExecutionException, InterruptedException {
        ExecutorService	executorService	= Executors.newFixedThreadPool(100);
//        ProxyHost proxy = new ProxyHost("proxy", 3128);
        URI uri = new URI(this.getServletConfig().getInitParameter("url"));
        jiraRestClient = new JiraRestClient(executorService);
        jiraRestClient.connect(uri, this.getServletConfig().getInitParameter("username"), this.getServletConfig().getInitParameter("password"));
    }
    public void AddCommentToIssue() throws URISyntaxException, IOException, RestException {
        CommentBean commentBean = new CommentBean();
        commentBean.setBody("This is a new comment via JiraRestClient.");
        boolean commentToIssue = jiraRestClient.getIssueClient().addCommentToIssue(ISSUEKEY_TO_SEARCH, commentBean);
        Assert.assertTrue(commentToIssue);
        System.out.println("发送成功");
    }
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test() {
        super();
        // TODO Auto-generated constructor stub
    }




	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			connect();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			AddCommentToIssue();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.getWriter().append("发送成功");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
